<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\Http\Requests\PatientBaseInfoRequest;
use App\Http\Requests\PatientUpdateRequest;
use App\Models\MedicalRecord;
use App\Models\Patient;
use App\Models\StMedicalNumber;
use App\Transformers\PatientEditTransformer;
use Illuminate\Http\Request;

class PatientController extends ApiController
{

    /**
     * 基本信息提交, 同时创建病历
     * @param \App\Http\Requests\PatientBaseInfoRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function store(PatientBaseInfoRequest $request)
    {
        $input = $request->all();

//        $input['password']    = bcrypt('123123');
        //患者未MD5
        $input['password']    = '1016171161711616017161016';
        $input['first_visit'] = $input['clinic_at'];

        if (1 == $request->user()->hospital->type) {
            $input['is_western'] = 1;
        } else {
            $input['is_western'] = 0;
        }

        \DB::beginTransaction();    //  开启事务处理

        try {
            //  存储
            $patient = Patient::create($input);

            $input['patient_id'] = $patient->id;
            $input['visit_type'] = 1;

            $record = MedicalRecord::create($input);

            \DB::commit();
        } catch (\Exception $e) {

            \DB::rollBack();
            throw $e;
        }

        return $this->response(['record_id' => $record->id, 'patient_id' => $patient->id]);
    }

    /**
     * 患者基本信息
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        $this->validate(
            $request,
            [
                'patient_id' => 'required|integer|min:1'
            ], [
                'patient_id.required' => '患者ID必须填写',
                'patient_id.integer'  => '患者ID必须是一个数字',
                'patient_id.min'      => '患者ID必须大于等于1',
            ]);
            $patient = Patient::findOrFail($request->get('patient_id'));
            $medical_number =StMedicalNumber::where(['patient_id'=>$patient->id,'hospital_id'=>$patient->hospital_id])->first();
            if($medical_number){
                $patient->medical_number = $medical_number->medical_number;
            }

        return $this->item($patient, new PatientEditTransformer());
    }

    public function update(PatientUpdateRequest $request, Patient $patient, MedicalRecord $record)
    {
        $input      = $request->except(['record_id', 'patient_id',]);
        $record_id  = $request->input('record_id');
        $patient_id = $request->input('patient_id');

        $input['first_visit'] = $input['clinic_at'];

        \DB::beginTransaction();    //  开启事务处理

        try {
            //  存储
            $patient->where(['id' => $patient_id])->first()->fill($input)->save();
            $record->where(['id' => $record_id])->first()->fill($input)->save();
            \DB::commit();
        } catch (\Exception $e) {

            \DB::rollBack();
            throw $e;
        }

        return $this->responseForUpdate(true);
    }
}
